<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 7.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16.ico">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" integrity="sha256-wiz7ZSCn/btzhjKDQBms9Hx4sSeUYsDrTLg7roPstac=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.css" integrity="sha256-6cQIC71/iBIYXFK+0RHAvwmjwWzkWd+r7v/BX3/vZDc=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/themes/green/pace-theme-minimal.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/pace.min.js" integrity="sha256-gqd7YTjg/BtfqWSwsJOvndl0Bxc8gFImLEkXQT8+qj0=" crossorigin="anonymous"></script>

<script class="next-config" data-name="main" type="application/json">{"hostname":"sumumm.github.io","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.19.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":"mac"},"fold":{"enable":true,"height":300},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":true,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="本文主要是天猫蓝牙Mesh开发——天猫精灵控制智能灯的相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:type" content="article">
<meta property="og:title" content="LV03-04-天猫蓝牙Mesh开发-05-天猫精灵控制智能灯">
<meta property="og:url" content="https://sumumm.github.io/post/29f1a077.html">
<meta property="og:site_name" content="苏木">
<meta property="og:description" content="本文主要是天猫蓝牙Mesh开发——天猫精灵控制智能灯的相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV21-%E5%A4%A9%E7%8C%AB%E7%B2%BE%E7%81%B5IOT/LV03-04-%E5%A4%A9%E7%8C%AB%E8%93%9D%E7%89%99Mesh%E5%BC%80%E5%8F%91-05-%E5%A4%A9%E7%8C%AB%E7%B2%BE%E7%81%B5%E6%8E%A7%E5%88%B6%E6%99%BA%E8%83%BD%E7%81%AF/img/image-20231225234403787.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV21-%E5%A4%A9%E7%8C%AB%E7%B2%BE%E7%81%B5IOT/LV03-04-%E5%A4%A9%E7%8C%AB%E8%93%9D%E7%89%99Mesh%E5%BC%80%E5%8F%91-05-%E5%A4%A9%E7%8C%AB%E7%B2%BE%E7%81%B5%E6%8E%A7%E5%88%B6%E6%99%BA%E8%83%BD%E7%81%AF/img/image-20231227213041935.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV21-%E5%A4%A9%E7%8C%AB%E7%B2%BE%E7%81%B5IOT/LV03-04-%E5%A4%A9%E7%8C%AB%E8%93%9D%E7%89%99Mesh%E5%BC%80%E5%8F%91-05-%E5%A4%A9%E7%8C%AB%E7%B2%BE%E7%81%B5%E6%8E%A7%E5%88%B6%E6%99%BA%E8%83%BD%E7%81%AF/img/image-20231227213226494.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV21-%E5%A4%A9%E7%8C%AB%E7%B2%BE%E7%81%B5IOT/LV03-04-%E5%A4%A9%E7%8C%AB%E8%93%9D%E7%89%99Mesh%E5%BC%80%E5%8F%91-05-%E5%A4%A9%E7%8C%AB%E7%B2%BE%E7%81%B5%E6%8E%A7%E5%88%B6%E6%99%BA%E8%83%BD%E7%81%AF/img/image-20231226074138417.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV21-%E5%A4%A9%E7%8C%AB%E7%B2%BE%E7%81%B5IOT/LV03-04-%E5%A4%A9%E7%8C%AB%E8%93%9D%E7%89%99Mesh%E5%BC%80%E5%8F%91-05-%E5%A4%A9%E7%8C%AB%E7%B2%BE%E7%81%B5%E6%8E%A7%E5%88%B6%E6%99%BA%E8%83%BD%E7%81%AF/img/image-20231226074305237.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV21-%E5%A4%A9%E7%8C%AB%E7%B2%BE%E7%81%B5IOT/LV03-04-%E5%A4%A9%E7%8C%AB%E8%93%9D%E7%89%99Mesh%E5%BC%80%E5%8F%91-05-%E5%A4%A9%E7%8C%AB%E7%B2%BE%E7%81%B5%E6%8E%A7%E5%88%B6%E6%99%BA%E8%83%BD%E7%81%AF/img/image-20231226074807652.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV21-%E5%A4%A9%E7%8C%AB%E7%B2%BE%E7%81%B5IOT/LV03-04-%E5%A4%A9%E7%8C%AB%E8%93%9D%E7%89%99Mesh%E5%BC%80%E5%8F%91-05-%E5%A4%A9%E7%8C%AB%E7%B2%BE%E7%81%B5%E6%8E%A7%E5%88%B6%E6%99%BA%E8%83%BD%E7%81%AF/img/image-20231226075918166.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV21-%E5%A4%A9%E7%8C%AB%E7%B2%BE%E7%81%B5IOT/LV03-04-%E5%A4%A9%E7%8C%AB%E8%93%9D%E7%89%99Mesh%E5%BC%80%E5%8F%91-05-%E5%A4%A9%E7%8C%AB%E7%B2%BE%E7%81%B5%E6%8E%A7%E5%88%B6%E6%99%BA%E8%83%BD%E7%81%AF/img/image-20231226080106730.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV21-%E5%A4%A9%E7%8C%AB%E7%B2%BE%E7%81%B5IOT/LV03-04-%E5%A4%A9%E7%8C%AB%E8%93%9D%E7%89%99Mesh%E5%BC%80%E5%8F%91-05-%E5%A4%A9%E7%8C%AB%E7%B2%BE%E7%81%B5%E6%8E%A7%E5%88%B6%E6%99%BA%E8%83%BD%E7%81%AF/img/image-20231226120612484.png">
<meta property="article:published_time" content="2024-01-07T03:45:42.000Z">
<meta property="article:modified_time" content="2025-06-13T16:25:57.027Z">
<meta property="article:author" content="苏木">
<meta property="article:tag" content="LV21-天猫精灵IOT">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV21-%E5%A4%A9%E7%8C%AB%E7%B2%BE%E7%81%B5IOT/LV03-04-%E5%A4%A9%E7%8C%AB%E8%93%9D%E7%89%99Mesh%E5%BC%80%E5%8F%91-05-%E5%A4%A9%E7%8C%AB%E7%B2%BE%E7%81%B5%E6%8E%A7%E5%88%B6%E6%99%BA%E8%83%BD%E7%81%AF/img/image-20231225234403787.png">


<link rel="canonical" href="https://sumumm.github.io/post/29f1a077.html">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://sumumm.github.io/post/29f1a077.html","path":"post/29f1a077.html","title":"LV03-04-天猫蓝牙Mesh开发-05-天猫精灵控制智能灯"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>LV03-04-天猫蓝牙Mesh开发-05-天猫精灵控制智能灯 | 苏木</title>
  








    <script src="/js/browser_tools_disable.js"></script>

  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<!-- hexo injector head_end start --><link rel="stylesheet" href="https://unpkg.com/hexo-next-tags-plus@latest/lib/tag_plus.css" media="defer" onload="this.media='all'"><!-- hexo injector head_end end --></head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">苏木</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">我的学习之路</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>苏木的家</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类页<span class="badge">42</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档页<span class="badge">673</span></a></li><li class="menu-item menu-item-flink"><a href="/flink/" rel="section"><i class="fa fa-link fa-fw"></i>友人帐</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于我</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%80%E3%80%81%E6%8E%A7%E5%88%B6%E7%81%AF%E7%9A%84%E5%BC%80%E5%85%B3"><span class="nav-text">一、控制灯的开关</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E7%B2%BE%E7%81%B5%E4%BA%8B%E4%BB%B6"><span class="nav-text">1. 精灵事件</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1-genie-event"><span class="nav-text">1.1 genie_event()</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-user-event"><span class="nav-text">1.2 user_event()</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-3-%E6%80%BB%E7%BB%93"><span class="nav-text">1.3 总结</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-LED%E6%8E%A7%E5%88%B6"><span class="nav-text">2. LED控制</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-light-ctl-default-c%E6%96%87%E4%BB%B6"><span class="nav-text">2.1 light_ctl_default.c文件</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-1-1-led-init"><span class="nav-text">2.1.1 _led_init()</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-1-2-led-set"><span class="nav-text">2.1.2 _led_set()</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-1-3-%E5%AE%8C%E6%95%B4%E4%BB%A3%E7%A0%81"><span class="nav-text">2.1.3 完整代码</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-init-light-para"><span class="nav-text">2.2 _init_light_para()</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-2-1-%E5%87%BD%E6%95%B0%E5%AE%9A%E4%B9%89"><span class="nav-text">2.2.1 函数定义</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-2-2-g-elem-state-%E6%95%B0%E7%BB%84"><span class="nav-text">2.2.2 g_elem_state[]数组</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3-element-models"><span class="nav-text">2.3 element_models[]</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-4-g-element-vendor-models"><span class="nav-text">2.4 g_element_vendor_models[]</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-5-elements"><span class="nav-text">2.5 elements[]</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-6-light-ctl-tc825x-c"><span class="nav-text">2.6 light_ctl_tc825x.c</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E5%B7%A5%E7%A8%8B%E4%BB%A3%E7%A0%81"><span class="nav-text">3. 工程代码</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-%E7%BC%96%E8%AF%91%E5%B7%A5%E7%A8%8B"><span class="nav-text">4. 编译工程</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-%E7%83%A7%E5%BD%95%E7%A8%8B%E5%BA%8F"><span class="nav-text">5. 烧录程序</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-%E5%AE%9E%E9%AA%8C%E7%8E%B0%E8%B1%A1"><span class="nav-text">6. 实验现象</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%8C%E3%80%81%E5%91%BC%E5%90%B8%E7%81%AF%E5%AE%9E%E7%8E%B0"><span class="nav-text">二、呼吸灯实现</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E4%BA%AE%E5%BA%A6%E6%A8%A1%E5%9E%8B%EF%BC%9F"><span class="nav-text">1. 亮度模型？</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E4%BB%A3%E7%A0%81%E4%BF%AE%E6%94%B9"><span class="nav-text">2. 代码修改</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E4%BA%91%E7%AB%AF%E5%B1%9E%E6%80%A7%E6%B7%BB%E5%8A%A0"><span class="nav-text">3. 云端属性添加</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-%E7%BC%96%E8%AF%91%E5%B7%A5%E7%A8%8B-1"><span class="nav-text">4. 编译工程</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-%E7%83%A7%E5%BD%95%E7%A8%8B%E5%BA%8F-1"><span class="nav-text">5. 烧录程序</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-%E5%AE%9E%E9%AA%8C%E7%8E%B0%E8%B1%A1-1"><span class="nav-text">6. 实验现象</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="苏木"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">苏木</p>
  <div class="site-description" itemprop="description">莫道桑榆晚，为霞尚满天</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">673</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">42</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">43</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/sumumm" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;sumumm" rel="noopener me" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://sumumm.github.io/post/29f1a077.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="苏木">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏木">
      <meta itemprop="description" content="莫道桑榆晚，为霞尚满天">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="LV03-04-天猫蓝牙Mesh开发-05-天猫精灵控制智能灯 | 苏木">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          LV03-04-天猫蓝牙Mesh开发-05-天猫精灵控制智能灯
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2024-01-07 11:45:42" itemprop="dateCreated datePublished" datetime="2024-01-07T11:45:42+08:00">2024-01-07</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/" itemprop="url" rel="index"><span itemprop="name">嵌入式开发</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/" itemprop="url" rel="index"><span itemprop="name">01HQ课程体系</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV21-%E5%A4%A9%E7%8C%AB%E7%B2%BE%E7%81%B5IOT/" itemprop="url" rel="index"><span itemprop="name">LV21-天猫精灵IOT</span></a>
        </span>
    </span>

  
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>5.7k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>21 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><p>本文主要是天猫蓝牙Mesh开发——天猫精灵控制智能灯的相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。</p>
<span id="more"></span>

<!-- Photo: https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV21-%E5%A4%A9%E7%8C%AB%E7%B2%BE%E7%81%B5IOT/LV03-04-%E5%A4%A9%E7%8C%AB%E8%93%9D%E7%89%99Mesh%E5%BC%80%E5%8F%91-05-%E5%A4%A9%E7%8C%AB%E7%B2%BE%E7%81%B5%E6%8E%A7%E5%88%B6%E6%99%BA%E8%83%BD%E7%81%AF/img/ -->

<details class="folding-tag" blue><summary> 点击查看使用工具及版本 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center" width=150px>Windows版本</td>        <td align="left">windows11</td>    </tr>    <tr>        <td align="center">Ubuntu版本</td>        <td align="left">Ubuntu22.04的64位版本</td>      </tr>    <tr>        <td align="center">VMware® Workstation 16 Pro</td>        <td align="left">16.2.3 build-19376536</td>      </tr>    <tr>        <td align="center">终端软件</td>        <td align="left">MobaXterm(Professional Edition v23.0 Build 5042 (license))</td>      </tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看本文参考资料 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center">分类</td>        <td align="center">网址</td>        <td align="center">说明</td>    </tr>    <tr>        <td align="center" rowspan="6">官方网站</td>        <td align="left"><a href="https://cn.aliyun.com/" target="_blank">阿里云</a></td>        <td align="left">阿里云官网主页</td>    </tr>    <tr>        <td align="left"><a href="https://www.aliyun.com/product/iot/livinglink?spm=5176.28508143.J_4VYgf18xNlTAyFFbOuOQe.317.5421154acZniIL&scm=20140722.X_data-1da961ea118f5bb0e4b2._.V_1" target="_blank">阿里生活物联平台</a></td>        <td align="left">生活物联网平台(飞燕平台)主页</td>    </tr>    <tr>        <td align="left"><a href="https://iap.aligenie.com/?spm=0.7629140.0.0.2c611780Ty6tzg" target="_blank">AliGenie</a></td>        <td align="left">天猫精灵开放平台AliGenie主页</td>    </tr>    <tr>        <td align="left"><a href="https://www.aliyun.com/product/iot/iot_instc_public_cn?spm=5176.28508143.J_4VYgf18xNlTAyFFbOuOQe.81.5421154au3ocqp&scm=20140722.X_data-9349178ba84cf028806a._.V_1" target="_blank">阿里物联网平台</a></td>        <td align="left">阿里物联网平台主页</td>    </tr>    <tr>        <td align="left"><a href="https://www.bluetooth.com/zh-cn/" target="_blank">Bluetooth 技术网站</a></td>        <td align="left">蓝牙协议规范什么的可以来这里找</td>    </tr>    <tr>        <td align="left"><a href="https://www.telink-semi.com/" target="_blank">Telink</a></td>        <td align="left">Telink | Chips for a Smarter IoT (telink-semi.com)<br><a href="https://www.telink-semi.cn/" target="_blank">Telink中文官网</a></td>    </tr>    <tr>        <td align="center" rowspan="5">开发手册</td>        <td align="left"><a href="https://help.aliyun.com/zh/alios-things/" target="_blank">AliOS Things开发指南</a></td>        <td align="left">AliOS Things开发指南，这里是最新版本，可以直接从官网找到</td>    </tr>     <tr>        <td align="left"><a href="https://haas.iot.aliyun.com/aliosthings/index.html" target="_blank">AliOS Things开发指南</a></td>        <td align="left">AliOS Things应用开发指南，这里应该是3.3版本的完整开发文档</td>    </tr>    <tr>        <td align="left"><a href="https://gaic.alicdn.com/aic/h5_daily/test/aliosthings/3.0.13/index.html" target="_blank">AliOS Things开发指南(3.0)</a></td>        <td align="left">AliOS Things应用开发指南，这里应该是3.0版本的完整开发文档</td>    </tr>    <tr>        <td align="left"><a href="https://help.aliyun.com/product/123207.html?spm=a2c4g.11186623.6.540.114e60d84sF58G" target="_blank">生活物联网平台开发文档</a></td>        <td align="left">生活物联网平台（飞燕平台）开发文档<br><a href="https://help.aliyun.com/document_detail/180778.html?spm=a2c4g.123207.0.0.49556adcO5apvd" target="_blank">《设备端开发指南》</a></td>    </tr>    <tr>        <td align="left"><a href="https://doc-bot.tmall.com/docs/doc.htm?spm=0.7629140.0.0.7bf81780UN7WkQ&treeId=578&articleId=118057&docType=1" target="_blank">Wi-Fi IoT品类定义与功能开发</a></td>        <td align="left">天猫精灵IoT开放平台——Wi-Fi IoT品类定义与功能开发</td>    </tr>    <tr>        <td align="center" rowspan="3">硬件平台</td>        <td align="left"><a href="https://developer.aliyun.com/article/761644?spm=a2c6h.12873581.0.dArticle761644.1c1b589dRHgvcR&groupCode=genieaiot" target="_blank">mk3080 WiFi开发板</a></td>        <td align="left">WiFi开发板使用指南-阿里云开发者社区</td>    </tr>    <tr>        <td align="left"><a href="http://www.taichi-maker.com/homepage/esp8266-nodemcu-iot/esp8266-nodemcu-tutorial-index/" target="_blank">esp8266开发板</a></td>        <td align="left">一个教程：ESP8266-NodeMCU开发板详解-太极创客 (taichi-maker.com)</td>    </tr>    <tr>        <td align="left"><a href="https://wiki.telink-semi.cn/doc/ds/DS_TLSR8258-E_Datasheet%20for%20Telink%20BLE+IEEE802.15.4%20Multi-Standard%20Wireless%20SoC%20TLSR8258.pdf" target="_blank">TLSR8258 Datasheet</a></td>        <td align="left">Datasheet for Telink BLE + IEEE802.15.4 MultiStandard Wireless SoC TLSR8258</td>    </tr>    <tr>        <td align="center" rowspan="2">参考资料</td>        <td align="left"><a href="https://developer.aliyun.com/article/720516#" target="_blank">AliOS Things 3.0 应用开发指南</a></td>        <td align="left">这个只是一篇参考文章，里面是一些环境搭建相关的，可以参考</td>    </tr>    <tr>        <td align="left"><a href="https://info.support.huawei.com/info-finder/encyclopedia/zh/index.html" target="_blank">IP知识百科 - 华为 (huawei.com)</a></td>        <td align="left">IP的一些相关知识点</td>    </tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看相关文件下载 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center" width=150px>分类</td>        <td align="center">网址</td>        <td align="center">说明</td>    </tr>    <tr>        <td align="left" rowspan="6">蓝牙规范相关文档</td>        <td align="left"><a href="https://www.bluetooth.com/zh-cn/specifications/specs/core-specification-5-2/" target="_blank">Core Specification 5.2</a></td>        <td align="left">核心规格 5.2，该规范定义了创建可互操作的Bluetooth 设备所需的技术。                        <br><a href="./蓝牙参考资料/Core_v5.2.pdf" target="_blank">《Core_v5.2.pdf》</a></td>      </tr>    <tr>        <td align="left"><a href="https://www.bluetooth.com/zh-cn/specifications/specs/mesh-model-1-1/" target="_blank">Mesh Model(v1.1)</a></td>        <td align="left">本Bluetooth 规范定义了模型（以及它们所需的状态和消息），这些模型用于在mesh 网络中的节点上执行基本功能，超出了Bluetooth Mesh 配置文件 规范中定义的基础模型。                        <br>本规范包括定义跨设备类型标准功能的通用模型，以及支持关键mesh 场景的模型，如照明控制、传感器、时间和场景。                        <br><a href="./蓝牙参考资料/MshMDL_v1.1.pdf" target="_blank">《MshMDL_v1.1.pdf》</a></td>      </tr>    <tr>        <td align="left"><a href="https://www.bluetooth.com/zh-cn/specifications/specs/mesh-profile-1-0-1/" target="_blank">Mesh Profile(v1.0.1)</a></td>        <td align="left">该Bluetooth 规范定义了基本要求，以实现可互操作的mesh 网络解决方案，用于Bluetooth 低能量无线技术。                        <br><a href="./蓝牙参考资料/MshPRFv1.0.1.pdf" target="_blank">《MshPRFv1.0.1.pdf》</a></td>      </tr>    <tr>        <td align="left"><a href="https://www.bluetooth.com/zh-cn/specifications/device-properties/" target="_blank">Mesh Device Properties</a></td>        <td align="left">本规范包含Bluetooth Mesh 配置文件 和Bluetooth Mesh 模型规范所要求的设备属性的定义。                        <br>但是跟之前的有些区别，我主要看的之前的版本：<a href="./蓝牙参考资料/MeshDeviceProperties_v1.2.pdf" target="_blank">《MMeshDeviceProperties_v1.2.pdf》</a></td>      </tr>    <tr>        <td align="left"><a href="https://www.bluetooth.com/specifications/gss/" target="_blank">GATT Specification Supplement</a></td>        <td align="left">GATT Specification Supplement | Bluetooth® Technology Website。                        <br>好像可以在线看：<a href="https://btprodspecificationrefs.blob.core.windows.net/gatt-specification-supplement/GATT_Specification_Supplement.pdf" target="_blank">《GATT Specification Supplement》</a></td>      </tr>     <tr>        <td align="left"><a href="https://www.bluetooth.com/specifications/assigned-numbers/" target="_blank">Assigned Numbers</a></td>        <td align="left">GATT的一些类型定义可以在这里找。</td>      </tr>    <tr>        <td align="left" rowspan="2">AliOS Things</td>        <td align="left"><a href="https://gitee.com/alios-things-admin/AliOS-Things" target="_blank">alios-things/AliOS-Things</a></td>        <td align="left">Gitee上的AliOSThings SDK源码仓库</td>      </tr>    <tr>        <td align="left"><a href="https://github.com/alibaba/AliOS-Things" target="_blank">alibaba/AliOS-Things</a></td>        <td align="left">GitHub上的AliOSThings SDK源码仓库</td>      </tr>    <tr>        <td align="left" rowspan="2">天猫精灵蓝牙Mesh协议栈</td>        <td align="left"><a href="https://github.com/alibaba-archive/genie-bt-mesh-stack" target="_blank">alibaba-archive/genie-bt-mesh-stack</a></td>        <td align="left">GitHub上的天猫精灵蓝牙Mesh协议栈源码仓库。<br>之前是在<a href="https://github.com/alibaba/genie-bt-mesh-stack"target="_blank">alibaba/genie-bt-mesh-stack</a>这个仓库。<br>写笔记的时候最新提交为<a href="https://github.com/alibaba-archive/genie-bt-mesh-stack/tree/faf523618a6a2560090fc423222b9db80984bb7a" target="_blank">faf523618a6a2560090fc423222b9db80984bb7a</a></td>      </tr>    <tr>        <td align="left"><a href="https://help.aliyun.com/document_detail/171552.html?spm=a2c4g.173310.0.0.55d6e992e8xJip" target="_blank">蓝牙Mesh设备开发指南</a></td>        <td align="left">阿里云生活服务平台开发手册——蓝牙设备开发一节中的内容</td>      </tr></table>
              </div>
            </details>


<h1 id="一、控制灯的开关"><a href="#一、控制灯的开关" class="headerlink" title="一、控制灯的开关"></a><font size=3>一、控制灯的开关</font></h1><h2 id="1-精灵事件"><a href="#1-精灵事件" class="headerlink" title="1. 精灵事件"></a><font size=3>1. 精灵事件</font></h2><h3 id="1-1-genie-event"><a href="#1-1-genie-event" class="headerlink" title="1.1 genie_event()"></a><font size=3>1.1 genie_event()</font></h3><p>我们找到这个函数，它定义在SDK源码顶层目录的genie_app&#x2F;base&#x2F;genie_event.c中，声明在对应的h文件中：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @brief The handler for the underlying events. If necessary</span></span><br><span class="line"><span class="comment"> *        this handler dispatch the user events to applications.</span></span><br><span class="line"><span class="comment"> * @param[in] event refers to the event details.</span></span><br><span class="line"><span class="comment"> * @param[in] args refers to the additional information for the event.</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">genie_event</span><span class="params">(E_GENIE_EVENT event, <span class="type">void</span> *args)</span>;</span><br></pre></td></tr></table></figure>

<p>可以看到这个函数是用于处理事件的，我们看一下都是怎么使用的在 genie_app&#x2F;bluetooth&#x2F;mesh&#x2F;genie_mesh.c中</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">static</span> <span class="type">void</span> _genie_mesh_ready(<span class="type">int</span> err)</span><br><span class="line">&#123;</span><br><span class="line">	<span class="comment">//......</span></span><br><span class="line">    <span class="comment">//send event</span></span><br><span class="line">    genie_event(GENIE_EVT_SDK_MESH_INIT, <span class="literal">NULL</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>可以看到这个函数传入了一个宏GENIE_EVT_SDK_MESH_INIT，这其实就是一个事件的宏，然后在函数中进行处理：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">genie_event</span><span class="params">(E_GENIE_EVENT event, <span class="type">void</span> *p_arg)</span></span><br><span class="line">&#123;</span><br><span class="line">    E_GENIE_EVENT next_event = event;</span><br><span class="line">    <span class="type">uint8_t</span> ignore_user_event = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">ifdef</span> CONFIG_MESH_MODEL_TRANS</span></span><br><span class="line">    <span class="keyword">if</span>(event != GENIE_EVT_SDK_TRANS_CYCLE) &#123;</span><br><span class="line">        GENIE_MESH_EVENT_PRINT(event);</span><br><span class="line">    &#125;</span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line">    <span class="keyword">switch</span>(event) &#123;</span><br><span class="line">        <span class="keyword">case</span> GENIE_EVT_SW_RESET:</span><br><span class="line">            <span class="comment">//call user_event first</span></span><br><span class="line">            user_event(GENIE_EVT_SW_RESET, p_arg);</span><br><span class="line">            ignore_user_event = <span class="number">1</span>;</span><br><span class="line">            next_event = _genie_event_handle_sw_reset();</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">		<span class="comment">//中间还有大量的宏定义，这里省略......</span></span><br><span class="line">        <span class="keyword">case</span> GENIE_EVT_SDK_MESH_INIT:</span><br><span class="line">            <span class="comment">//update p_arg to user_event</span></span><br><span class="line">            p_arg = (<span class="type">void</span> *)&amp;g_elem_state[<span class="number">0</span>];</span><br><span class="line">            next_event = _genie_event_handle_mesh_init();</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">		<span class="comment">//中间还有大量的宏定义，这里省略......</span></span><br><span class="line">        <span class="keyword">default</span>:</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span>(!ignore_user_event)</span><br><span class="line">        user_event(event, p_arg);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span>(next_event != event) &#123;</span><br><span class="line">        genie_event(next_event, p_arg);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>可以看到这里面有大量的case，每一个case里面会执行对应的分支。而且，在最下面，每一个分支执行完毕之后，若是ignore_user_event为0，都还会执行一个user_event()。</p>
<h3 id="1-2-user-event"><a href="#1-2-user-event" class="headerlink" title="1.2 user_event()"></a><font size=3>1.2 user_event()</font></h3><p>我们找一下它的定义，会发现，它定义在我们的demo中（如app&#x2F;example&#x2F;bluetooth&#x2F;light_ctl_demo&#x2F;light_ctl_demo.c）：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">void</span> <span class="title function_">user_event</span><span class="params">(E_GENIE_EVENT event, <span class="type">void</span> *p_arg)</span></span><br><span class="line">&#123;</span><br><span class="line">    E_GENIE_EVENT next_event = event;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//BT_DBG(&quot;%s, %s %p\n&quot;, __func__, genie_event_str[event], p_arg);</span></span><br><span class="line">    <span class="keyword">switch</span>(event) &#123;</span><br><span class="line">        <span class="keyword">case</span> GENIE_EVT_SW_RESET:</span><br><span class="line">        <span class="keyword">case</span> GENIE_EVT_HW_RESET_START:</span><br><span class="line">            _led_flash(<span class="number">5</span>, <span class="number">1</span>);</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">case</span> GENIE_EVT_HW_RESET_DONE:</span><br><span class="line">            _reset_light_para();</span><br><span class="line">            BT_DBG(<span class="string">&quot;GENIE_EVT_HW_RESET_DONE\n&quot;</span>);</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">case</span> GENIE_EVT_SDK_MESH_INIT:</span><br><span class="line"><span class="meta">#<span class="keyword">if</span> defined(BOARD_TG7100B) || defined(BOARD_CH6121EVB)</span></span><br><span class="line">            _user_init();</span><br><span class="line"><span class="meta">#<span class="keyword">else</span></span></span><br><span class="line">            _led_init();</span><br><span class="line">            _init_light_para();</span><br><span class="line">            _user_init();</span><br><span class="line">            <span class="keyword">if</span> (!genie_reset_get_flag()) &#123;</span><br><span class="line">                next_event = GENIE_EVT_SDK_ANALYZE_MSG;</span><br><span class="line">            &#125;</span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">case</span> GENIE_EVT_SDK_MESH_PROV_SUCCESS:</span><br><span class="line">            _led_flash(<span class="number">3</span>, <span class="number">0</span>);</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">case</span> GENIE_EVT_SDK_TRANS_CYCLE:</span><br><span class="line">        <span class="keyword">case</span> GENIE_EVT_SDK_ACTION_DONE:</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="type">elem_state_t</span> *p_elem = (<span class="type">elem_state_t</span> *)p_arg;</span><br><span class="line">            _led_ctrl(p_elem);</span><br><span class="line">            <span class="keyword">if</span>(event == GENIE_EVT_SDK_ACTION_DONE)</span><br><span class="line">                _save_light_state(p_elem);</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">case</span> GENIE_EVT_SDK_INDICATE:</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">case</span> GENIE_EVT_SDK_VENDOR_MSG:</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">default</span>:</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">if</span>(next_event != event) &#123;</span><br><span class="line">        genie_event(next_event, p_arg);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>这里也是一堆的case分支，每个分支会去处理不同的事，我们看到有这么一个分支GENIE_EVT_SDK_MESH_INIT，是不是很熟悉，上面的genie_event也有，也就是说，上面调用了genie_event()之后，就会进入user_event()进行处理。</p>
<h3 id="1-3-总结"><a href="#1-3-总结" class="headerlink" title="1.3 总结"></a><font size=3>1.3 总结</font></h3><p>从上面的分析可知，当出现有genie_event()时候会处理一些内容，然后就会调用user_event()去处理用户自定义事件。</p>
<h2 id="2-LED控制"><a href="#2-LED控制" class="headerlink" title="2. LED控制"></a><font size=3>2. LED控制</font></h2><h3 id="2-1-light-ctl-default-c文件"><a href="#2-1-light-ctl-default-c文件" class="headerlink" title="2.1 light_ctl_default.c文件"></a><font size=3>2.1 light_ctl_default.c文件</font></h3><p>这里有坑，这只是第一次写的时候踩的，就放在这里吧。后面直接看2.6节即可。</p>
<h4 id="2-1-1-led-init"><a href="#2-1-1-led-init" class="headerlink" title="2.1.1 _led_init()"></a><font size=3>2.1.1 _led_init()</font></h4><p>这个函数在app&#x2F;example&#x2F;bluetooth&#x2F;light_ctl_demo&#x2F;light_ctl_default.c中定义，我们在这里完成这个初始化：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">define</span> LED0_PIN TC825X_GET_PIN_NUM(GPIO_PB4) <span class="comment">// RED——PC1</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> LED1_PIN TC825X_GET_PIN_NUM(GPIO_PB5) <span class="comment">// GREEN——PC1</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> LED2_PIN TC825X_GET_PIN_NUM(GPIO_PC1) <span class="comment">// BLUE——PC1</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">static</span> <span class="type">gpio_dev_t</span> led_dev[<span class="number">3</span>] = &#123;<span class="number">0</span>&#125;;</span><br><span class="line"></span><br><span class="line"><span class="type">static</span> <span class="type">void</span> _led_init(<span class="type">void</span>)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int32_t</span> ret = <span class="number">0</span>;</span><br><span class="line">    led_dev[<span class="number">0</span>].port = LED0_PIN; <span class="comment">/* gpio port config */</span></span><br><span class="line">    led_dev[<span class="number">0</span>].config = OUTPUT_PUSH_PULL;<span class="comment">/* set as output mode */</span></span><br><span class="line">    ret = hal_gpio_init(&amp;led_dev[<span class="number">0</span>]); <span class="comment">/* configure GPIO with the given settings */</span></span><br><span class="line">    <span class="keyword">if</span> (ret != <span class="number">0</span>) &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;gpio init error !\n&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    led_dev[<span class="number">1</span>].port = LED1_PIN; <span class="comment">/* gpio port config */</span></span><br><span class="line">    led_dev[<span class="number">1</span>].config = OUTPUT_PUSH_PULL;<span class="comment">/* set as output mode */</span></span><br><span class="line">    ret = hal_gpio_init(&amp;led_dev[<span class="number">1</span>]); <span class="comment">/* configure GPIO with the given settings */</span></span><br><span class="line">    <span class="keyword">if</span> (ret != <span class="number">0</span>) &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;gpio init error !\n&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    led_dev[<span class="number">2</span>].port = LED2_PIN; <span class="comment">/* gpio port config */</span></span><br><span class="line">    led_dev[<span class="number">2</span>].config = OUTPUT_PUSH_PULL;<span class="comment">/* set as output mode */</span></span><br><span class="line">    ret = hal_gpio_init(&amp;led_dev[<span class="number">2</span>]); <span class="comment">/* configure GPIO with the given settings */</span></span><br><span class="line">    <span class="keyword">if</span> (ret != <span class="number">0</span>) &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;gpio init error !\n&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;++++++++++ led init! ++++++++++\n&quot;</span>);</span><br><span class="line">    <span class="keyword">return</span> ret;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="2-1-2-led-set"><a href="#2-1-2-led-set" class="headerlink" title="2.1.2 _led_set()"></a><font size=3>2.1.2 _led_set()</font></h4><p>这个函数在app&#x2F;example&#x2F;bluetooth&#x2F;light_ctl_demo&#x2F;light_ctl_default.c中定义，我们在这里完成这个led状态设置：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">static</span> <span class="type">void</span> _led_set(<span class="type">uint8_t</span> onoff, <span class="type">uint16_t</span> actual, <span class="type">uint16_t</span> temperature)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">if</span>(onoff)</span><br><span class="line">    &#123;</span><br><span class="line">        hal_gpio_output_high(&amp;led_dev[<span class="number">0</span>]);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">else</span></span><br><span class="line">    &#123;</span><br><span class="line">        hal_gpio_output_low(&amp;led_dev[<span class="number">0</span>]);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="2-1-3-完整代码"><a href="#2-1-3-完整代码" class="headerlink" title="2.1.3 完整代码"></a><font size=3>2.1.3 完整代码</font></h4><details class="folding-tag" blue><summary> 点击查看详情 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;aos/aos.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;hal/soc/gpio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;drivers/8258/gpio_8258.h&quot;</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> LED0_PIN TC825X_GET_PIN_NUM(GPIO_PB4) <span class="comment">// RED——PC1</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> LED1_PIN TC825X_GET_PIN_NUM(GPIO_PB5) <span class="comment">// GREEN——PC1</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> LED2_PIN TC825X_GET_PIN_NUM(GPIO_PC1) <span class="comment">// BLUE——PC1</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">static</span> <span class="type">gpio_dev_t</span> led_dev[<span class="number">3</span>] = &#123;<span class="number">0</span>&#125;;</span><br><span class="line"></span><br><span class="line"><span class="type">static</span> <span class="type">void</span> _led_init(<span class="type">void</span>)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int32_t</span> ret = <span class="number">0</span>;</span><br><span class="line">    led_dev[<span class="number">0</span>].port = LED0_PIN; <span class="comment">/* gpio port config */</span></span><br><span class="line">    led_dev[<span class="number">0</span>].config = OUTPUT_PUSH_PULL;<span class="comment">/* set as output mode */</span></span><br><span class="line">    ret = hal_gpio_init(&amp;led_dev[<span class="number">0</span>]); <span class="comment">/* configure GPIO with the given settings */</span></span><br><span class="line">    <span class="keyword">if</span> (ret != <span class="number">0</span>) &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;gpio init error !\n&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    led_dev[<span class="number">1</span>].port = LED1_PIN; <span class="comment">/* gpio port config */</span></span><br><span class="line">    led_dev[<span class="number">1</span>].config = OUTPUT_PUSH_PULL;<span class="comment">/* set as output mode */</span></span><br><span class="line">    ret = hal_gpio_init(&amp;led_dev[<span class="number">1</span>]); <span class="comment">/* configure GPIO with the given settings */</span></span><br><span class="line">    <span class="keyword">if</span> (ret != <span class="number">0</span>) &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;gpio init error !\n&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    led_dev[<span class="number">2</span>].port = LED2_PIN; <span class="comment">/* gpio port config */</span></span><br><span class="line">    led_dev[<span class="number">2</span>].config = OUTPUT_PUSH_PULL;<span class="comment">/* set as output mode */</span></span><br><span class="line">    ret = hal_gpio_init(&amp;led_dev[<span class="number">2</span>]); <span class="comment">/* configure GPIO with the given settings */</span></span><br><span class="line">    <span class="keyword">if</span> (ret != <span class="number">0</span>) &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;gpio init error !\n&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;++++++++++ led init! ++++++++++\n&quot;</span>);</span><br><span class="line">    <span class="keyword">return</span> ret;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">static</span> <span class="type">void</span> _led_set(<span class="type">uint8_t</span> onoff, <span class="type">uint16_t</span> actual, <span class="type">uint16_t</span> temperature)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">if</span>(onoff)</span><br><span class="line">    &#123;</span><br><span class="line">        hal_gpio_output_high(&amp;led_dev[<span class="number">0</span>]);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">else</span></span><br><span class="line">    &#123;</span><br><span class="line">        hal_gpio_output_low(&amp;led_dev[<span class="number">0</span>]);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="2-2-init-light-para"><a href="#2-2-init-light-para" class="headerlink" title="2.2 _init_light_para()"></a><font size=3>2.2 _init_light_para()</font></h3><h4 id="2-2-1-函数定义"><a href="#2-2-1-函数定义" class="headerlink" title="2.2.1 函数定义"></a><font size=3>2.2.1 函数定义</font></h4><p>这个函数暂时不需要修改，这里需要了解一下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">static</span> <span class="type">void</span> _init_light_para(<span class="type">void</span>)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">uint8_t</span> i = <span class="number">0</span>;</span><br><span class="line">    E_GENIE_FLASH_ERRCODE ret;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// init element state</span></span><br><span class="line">    <span class="built_in">memset</span>(g_elem_state, <span class="number">0</span>, <span class="keyword">sizeof</span>(g_elem_state));</span><br><span class="line">    elem_state_init(MESH_ELEM_STATE_COUNT, g_elem_state);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// load light para</span></span><br><span class="line">    ret = genie_flash_read_userdata(GFI_MESH_POWERUP, (<span class="type">uint8_t</span> *)g_powerup, <span class="keyword">sizeof</span>(g_powerup));</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">if</span>(ret == GENIE_FLASH_SUCCESS) &#123;</span><br><span class="line">        <span class="keyword">while</span>(i &lt; MESH_ELEM_STATE_COUNT) &#123;</span><br><span class="line"><span class="meta">#<span class="keyword">ifdef</span> CONFIG_GENIE_OTA</span></span><br><span class="line">            <span class="comment">// if the device reboot by ota, it must be off.</span></span><br><span class="line">            <span class="keyword">if</span>(g_powerup[<span class="number">0</span>].last_onoff == <span class="number">0</span>) &#123;</span><br><span class="line">                g_elem_state[<span class="number">0</span>].powerup.last_onoff = g_powerup[<span class="number">0</span>].last_onoff;</span><br><span class="line">                g_elem_state[<span class="number">0</span>].state.onoff[T_TAR] = <span class="number">0</span>;</span><br><span class="line">                <span class="comment">// load lightness</span></span><br><span class="line">                <span class="keyword">if</span>(g_powerup[<span class="number">0</span>].last_actual) &#123;</span><br><span class="line">                    g_elem_state[<span class="number">0</span>].state.actual[T_TAR] = g_powerup[<span class="number">0</span>].last_actual;</span><br><span class="line">                    g_elem_state[<span class="number">0</span>].powerup.last_actual = g_powerup[<span class="number">0</span>].last_actual;</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="comment">// load temperature</span></span><br><span class="line">                <span class="keyword">if</span>(g_powerup[<span class="number">0</span>].last_temp) &#123;</span><br><span class="line">                    g_elem_state[<span class="number">0</span>].state.temp[T_TAR] = g_powerup[<span class="number">0</span>].last_temp;</span><br><span class="line">                    g_elem_state[<span class="number">0</span>].powerup.last_temp = g_powerup[<span class="number">0</span>].last_temp;</span><br><span class="line">                &#125;</span><br><span class="line">                clear_trans_para(&amp;g_elem_state[<span class="number">0</span>]);</span><br><span class="line">            &#125; <span class="keyword">else</span></span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line">            &#123;</span><br><span class="line">                <span class="built_in">memcpy</span>(&amp;g_elem_state[<span class="number">0</span>].powerup, &amp;g_powerup[<span class="number">0</span>], <span class="keyword">sizeof</span>(<span class="type">model_powerup_t</span>));</span><br><span class="line">                <span class="comment">// load lightness</span></span><br><span class="line">                <span class="keyword">if</span>(g_powerup[<span class="number">0</span>].last_actual) &#123;</span><br><span class="line">                    g_elem_state[<span class="number">0</span>].state.actual[T_TAR] = g_powerup[<span class="number">0</span>].last_actual;</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="comment">// load temperature</span></span><br><span class="line">                <span class="keyword">if</span>(g_powerup[<span class="number">0</span>].last_temp) &#123;</span><br><span class="line">                    g_elem_state[<span class="number">0</span>].state.temp[T_TAR] = g_powerup[<span class="number">0</span>].last_temp;</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="comment">//LIGHT_DBG(&quot;l:%d t:%d&quot;, g_powerup[0].last_actual, g_powerup[0].last_temp);</span></span><br><span class="line"></span><br><span class="line">                <span class="comment">// cal transition</span></span><br><span class="line">                <span class="keyword">if</span>(g_elem_state[<span class="number">0</span>].state.onoff[T_TAR] == <span class="number">1</span>) &#123;</span><br><span class="line">                    g_elem_state[<span class="number">0</span>].state.trans_start_time = k_uptime_get() + g_elem_state[<span class="number">0</span>].state.delay * <span class="number">5</span>;</span><br><span class="line">                    g_elem_state[<span class="number">0</span>].state.trans_end_time = g_elem_state[<span class="number">0</span>].state.trans_start_time + get_transition_time(g_elem_state[<span class="number">0</span>].state.trans);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            g_elem_state[<span class="number">0</span>].state.temp[T_CUR] = g_elem_state[<span class="number">0</span>].state.temp[T_TAR];</span><br><span class="line"></span><br><span class="line">            i++;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//LIGHT_DBG(&quot;done&quot;);</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>这里比较重要的是这个g_elem_state数组</p>
<h4 id="2-2-2-g-elem-state-数组"><a href="#2-2-2-g-elem-state-数组" class="headerlink" title="2.2.2 g_elem_state[]数组"></a><font size=3>2.2.2 g_elem_state[]数组</font></h4><p>这个数组的定义为：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">define</span> MESH_ELEM_COUNT 1</span></span><br><span class="line"><span class="type">elem_state_t</span> g_elem_state[MESH_ELEM_STATE_COUNT];</span><br></pre></td></tr></table></figure>

<p>它表示的事蓝牙Mesh中“元素”的状态，这个数组中每个元素（与前面的元素含义不同，这里指数组的元素）的数据类型为：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span>&#123;</span></span><br><span class="line">    <span class="type">u8_t</span> elem_index;</span><br><span class="line">    <span class="type">model_state_t</span> state;</span><br><span class="line">    <span class="type">model_powerup_t</span> powerup;</span><br><span class="line">    <span class="type">void</span> *user_data;</span><br><span class="line">&#125; <span class="type">elem_state_t</span>;</span><br></pre></td></tr></table></figure>

<p>powerup是用来干嘛的？我们来看一下它的定义：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span>&#123;</span></span><br><span class="line"><span class="meta">#<span class="keyword">ifdef</span> CONFIG_MESH_MODEL_GEN_ONOFF_SRV</span></span><br><span class="line">        <span class="type">u8_t</span> last_onoff;</span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">ifdef</span> CONFIG_MESH_MODEL_LIGHTNESS_SRV</span></span><br><span class="line">    <span class="type">u16_t</span> last_actual;</span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line"><span class="comment">//......</span></span><br><span class="line">&#125;  <span class="type">model_powerup_t</span>;</span><br></pre></td></tr></table></figure>

<p>可以看到里面是一些变量，开了宏之后才会使用，那么具体有什么用？假设我们现在使用天猫精灵开了灯，不巧的是，停电了，那么再有电的时候，我们的灯是不是应该保持停电前的状态呢？为了保存掉电之前的状态，就会把相关的内容保存在这里，保存在这个结构体中的数据会被写入到flash中，重新上电的时候会从flash读取对应的数据，恢复原来的状态。所以这里会有一个全局变量数组，它的成员数量与g_elem_state保持一致，每个“元素”都要有这样的一个保存状态的东西：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">model_powerup_t</span> g_powerup[MESH_ELEM_STATE_COUNT];</span><br></pre></td></tr></table></figure>

<h3 id="2-3-element-models"><a href="#2-3-element-models" class="headerlink" title="2.3 element_models[]"></a><font size=3>2.3 element_models[]</font></h3><p>app&#x2F;example&#x2F;bluetooth&#x2F;light_ctl_demo&#x2F;light_ctl_demo.c中还有这样一个模型的数组：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">static</span> <span class="class"><span class="keyword">struct</span> <span class="title">bt_mesh_model</span> <span class="title">element_models</span>[] =</span> &#123;</span><br><span class="line">    <span class="comment">// 这两个必须加上</span></span><br><span class="line">    BT_MESH_MODEL_CFG_SRV(),</span><br><span class="line">    BT_MESH_MODEL_HEALTH_SRV(),</span><br><span class="line"></span><br><span class="line">    MESH_MODEL_GEN_ONOFF_SRV(&amp;g_elem_state[<span class="number">0</span>]), <span class="comment">// 通用的开关模型</span></span><br><span class="line">    MESH_MODEL_LIGHTNESS_SRV(&amp;g_elem_state[<span class="number">0</span>]),</span><br><span class="line">    MESH_MODEL_CTL_SRV(&amp;g_elem_state[<span class="number">0</span>]),</span><br><span class="line"><span class="meta">#<span class="keyword">ifndef</span> CONFIG_ALI_SIMPLE_MODLE</span></span><br><span class="line">    MESH_MODEL_GEN_LEVEL_SRV(&amp;g_elem_state[<span class="number">0</span>]),</span><br><span class="line">    MESH_MODEL_CTL_SETUP_SRV(&amp;g_elem_state[<span class="number">0</span>]),</span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line"></span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>前面我们知道，这里的模型起始就是对应功能，其实网页好像也叫模型<a target="_blank" rel="noopener" href="https://living.aliyun.com/project/a123wb9unTW7xtGD/dev/product/a1RaVAkHa9k/productdetail">产品管理 - 生活物联网平台 (aliyun.com)——功能定义</a>：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV21-%E5%A4%A9%E7%8C%AB%E7%B2%BE%E7%81%B5IOT/LV03-04-%E5%A4%A9%E7%8C%AB%E8%93%9D%E7%89%99Mesh%E5%BC%80%E5%8F%91-05-%E5%A4%A9%E7%8C%AB%E7%B2%BE%E7%81%B5%E6%8E%A7%E5%88%B6%E6%99%BA%E8%83%BD%E7%81%AF/img/image-20231225234403787.png" alt="image-20231225234403787" />

<p>在阿里云上的物模型的定义为：物模型是对设备在云端的功能描述，包括设备的属性、服务和事件。物联网平台通过定义一种物的描述语言来描述物模型，称之为 TSL（即 Thing Specification Language），采用 JSON 格式，您可以根据 TSL 组装上报设备的数据。您可以导出完整物模型，用于云端应用开发；您也可以只导出精简物模型，配合设备端 SDK 实现设备开发。</p>
<p>所以说，这里面的开关属性，就是代码中的MESH_MODEL_GEN_ONOFF_SRV()模型。需要注意的是，一个元素可以有多个模型，也就是多个功能，我们只要放在这里就可以啦，后面会有地方调用到的。</p>
<h3 id="2-4-g-element-vendor-models"><a href="#2-4-g-element-vendor-models" class="headerlink" title="2.4 g_element_vendor_models[]"></a><font size=3>2.4 g_element_vendor_models[]</font></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">static</span> <span class="class"><span class="keyword">struct</span> <span class="title">bt_mesh_model</span> <span class="title">g_element_vendor_models</span>[] =</span> &#123;</span><br><span class="line">    MESH_MODEL_VENDOR_SRV(&amp;g_elem_state[<span class="number">0</span>]),</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>这个是通用厂商模型，比如我们要定义一些特殊的功能，这些功能在阿里云生活服务平台没有，就需要用到这个来进行自定义啦。</p>
<h3 id="2-5-elements"><a href="#2-5-elements" class="headerlink" title="2.5 elements[]"></a><font size=3>2.5 elements[]</font></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">bt_mesh_elem</span> <span class="title">elements</span>[] =</span> &#123;</span><br><span class="line">    BT_MESH_ELEM(<span class="number">0</span>, element_models, g_element_vendor_models, <span class="number">0</span>),</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>这里就是元素啦，这里可以算是元素的各种信息的填充，</p>
<h3 id="2-6-light-ctl-tc825x-c"><a href="#2-6-light-ctl-tc825x-c" class="headerlink" title="2.6 light_ctl_tc825x.c"></a><font size=3>2.6 light_ctl_tc825x.c</font></h3><p>我们按照2.1小节去修改对应的文件，定义对应的函数，会发现，这里巨坑，这个源文件似乎并没有参与编译(light_ctl_demo.mk中根本没有这个文件)，然后一看还有一个light_ctl_tc825x.c文件，里面也有定义了led相关函数，可是最终调用的是哪里的函数？我们看一下light_ctl_demo.c文件的最后面：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV21-%E5%A4%A9%E7%8C%AB%E7%B2%BE%E7%81%B5IOT/LV03-04-%E5%A4%A9%E7%8C%AB%E8%93%9D%E7%89%99Mesh%E5%BC%80%E5%8F%91-05-%E5%A4%A9%E7%8C%AB%E7%B2%BE%E7%81%B5%E6%8E%A7%E5%88%B6%E6%99%BA%E8%83%BD%E7%81%AF/img/image-20231227213041935.png" alt="image-20231227213041935" />

<p>会发现这里通过include来包含了c文件，我们搜一下是否有定义BOARD_TC825X这个宏：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV21-%E5%A4%A9%E7%8C%AB%E7%B2%BE%E7%81%B5IOT/LV03-04-%E5%A4%A9%E7%8C%AB%E8%93%9D%E7%89%99Mesh%E5%BC%80%E5%8F%91-05-%E5%A4%A9%E7%8C%AB%E7%B2%BE%E7%81%B5%E6%8E%A7%E5%88%B6%E6%99%BA%E8%83%BD%E7%81%AF/img/image-20231227213226494.png" alt="image-20231227213226494" />

<p>会发现是有定义的，所以我们实际使用的是这个文件中的函数，我们修改这个文件如下，这里相对于原文件，我只修改了一部分的内容。</p>
<details class="folding-tag" blue><summary> 点击查看详情 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;drivers/8258/gpio_8258.h&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;vendor/common/alios_app_config.h&quot;</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">ifdef</span> CONIFG_LIGHT_HONGYAN</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> WARM_PIN            TC825X_GET_PIN_NUM(GPIO_PB0)</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> COLD_PIN            TC825X_GET_PIN_NUM(GPIO_PB1)</span></span><br><span class="line"><span class="meta">#<span class="keyword">else</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> WARM_PIN            TC825X_GET_PIN_NUM(PWM_R)</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> COLD_PIN            TC825X_GET_PIN_NUM(PWM_G)</span></span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span>  LIGHT_FREQ       32000</span></span><br><span class="line"></span><br><span class="line"><span class="type">static</span> <span class="type">pwm_dev_t</span> light_led_c;</span><br><span class="line"><span class="type">static</span> <span class="type">pwm_dev_t</span> light_led_w;</span><br><span class="line"></span><br><span class="line"><span class="type">uint16_t</span> duty_list[] = &#123;</span><br><span class="line">    <span class="meta">#<span class="keyword">include</span> <span class="string">&quot;duty_list.h&quot;</span></span></span><br><span class="line">&#125;;</span><br><span class="line"><span class="meta">#<span class="keyword">define</span> LED0_PIN TC825X_GET_PIN_NUM(GPIO_PB4) <span class="comment">// RED——PC1</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> LED1_PIN TC825X_GET_PIN_NUM(GPIO_PB5) <span class="comment">// GREEN——PC1</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> LED2_PIN TC825X_GET_PIN_NUM(GPIO_PC1) <span class="comment">// BLUE——PC1</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">static</span> <span class="type">gpio_dev_t</span> led_dev[<span class="number">3</span>] = &#123;<span class="number">0</span>&#125;;</span><br><span class="line"></span><br><span class="line"><span class="type">static</span> <span class="type">void</span> _led_init(<span class="type">void</span>)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int32_t</span> ret = <span class="number">0</span>;</span><br><span class="line">    led_dev[<span class="number">0</span>].port = LED0_PIN; <span class="comment">/* gpio port config */</span></span><br><span class="line">    led_dev[<span class="number">0</span>].config = OUTPUT_PUSH_PULL;<span class="comment">/* set as output mode */</span></span><br><span class="line">    ret = hal_gpio_init(&amp;led_dev[<span class="number">0</span>]); <span class="comment">/* configure GPIO with the given settings */</span></span><br><span class="line">    <span class="keyword">if</span> (ret != <span class="number">0</span>) &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;gpio init error !\n&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    led_dev[<span class="number">1</span>].port = LED1_PIN; <span class="comment">/* gpio port config */</span></span><br><span class="line">    led_dev[<span class="number">1</span>].config = OUTPUT_PUSH_PULL;<span class="comment">/* set as output mode */</span></span><br><span class="line">    ret = hal_gpio_init(&amp;led_dev[<span class="number">1</span>]); <span class="comment">/* configure GPIO with the given settings */</span></span><br><span class="line">    <span class="keyword">if</span> (ret != <span class="number">0</span>) &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;gpio init error !\n&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    led_dev[<span class="number">2</span>].port = LED2_PIN; <span class="comment">/* gpio port config */</span></span><br><span class="line">    led_dev[<span class="number">2</span>].config = OUTPUT_PUSH_PULL;<span class="comment">/* set as output mode */</span></span><br><span class="line">    ret = hal_gpio_init(&amp;led_dev[<span class="number">2</span>]); <span class="comment">/* configure GPIO with the given settings */</span></span><br><span class="line">    <span class="keyword">if</span> (ret != <span class="number">0</span>) &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;gpio init error !\n&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;++++++++++ led init! ++++++++++\n&quot;</span>);</span><br><span class="line">    <span class="keyword">return</span> ret;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//temperature 800~20000</span></span><br><span class="line"><span class="comment">//ligntness 655~65535</span></span><br><span class="line"><span class="comment">//return duty 0-100</span></span><br><span class="line"><span class="type">static</span> <span class="type">void</span> _get_led_duty(<span class="type">uint8_t</span> *p_duty, <span class="type">uint16_t</span> actual, <span class="type">uint16_t</span> temperature)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">uint8_t</span> cold = <span class="number">0</span>;</span><br><span class="line">    <span class="type">uint8_t</span> warm = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span>(temperature &gt; LIGHT_CTL_TEMP_MAX) &#123;</span><br><span class="line">        temperature = LIGHT_CTL_TEMP_MAX;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span>(temperature &lt; LIGHT_CTL_TEMP_MIN) &#123;</span><br><span class="line">        temperature = LIGHT_CTL_TEMP_MIN;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//0-100</span></span><br><span class="line">    cold = (temperature - LIGHT_CTL_TEMP_MIN) * <span class="number">100</span> / (LIGHT_CTL_TEMP_MAX - LIGHT_CTL_TEMP_MIN);</span><br><span class="line">    warm = <span class="number">100</span> - cold;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//0-100</span></span><br><span class="line">    p_duty[LED_COLD_CHANNEL] = (actual * cold) / <span class="number">65500</span>;</span><br><span class="line">    p_duty[LED_WARM_CHANNEL] = (actual * warm) / <span class="number">65500</span>;</span><br><span class="line">    <span class="keyword">if</span>(p_duty[LED_COLD_CHANNEL] == <span class="number">0</span> &amp;&amp; p_duty[LED_WARM_CHANNEL] == <span class="number">0</span>) &#123;</span><br><span class="line">        <span class="keyword">if</span>(temperature &gt; (LIGHT_CTL_TEMP_MAX - LIGHT_CTL_TEMP_MIN)&gt;&gt;<span class="number">1</span>) &#123;</span><br><span class="line">            p_duty[LED_COLD_CHANNEL] = <span class="number">1</span>;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            p_duty[LED_WARM_CHANNEL] = <span class="number">1</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//LIGHT_DBG(&quot;%d %d [%d %d] [%d %d]&quot;, actual, temperature, warm, cold, p_duty[LED_COLD_CHANNEL], p_duty[LED_WARM_CHANNEL]);</span></span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">static</span> <span class="type">int</span> _set_pwm_duty(<span class="type">uint8_t</span> channel, <span class="type">uint8_t</span> duty)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> err = <span class="number">-1</span>;</span><br><span class="line">    <span class="type">pwm_config_t</span> pwm_cfg;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span>(duty &gt; <span class="number">100</span>) &#123;</span><br><span class="line">        LIGHT_DBG(<span class="string">&quot;invaild&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    pwm_cfg.freq = LIGHT_FREQ;</span><br><span class="line">    pwm_cfg.duty_cycle = (<span class="type">float</span>)duty_list[duty]/duty_list[<span class="number">100</span>];</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (channel == LED_COLD_CHANNEL) &#123;</span><br><span class="line">        err = hal_pwm_para_chg(&amp;light_led_c, pwm_cfg);</span><br><span class="line">        <span class="keyword">if</span> (err) &#123;</span><br><span class="line">            LIGHT_DBG(<span class="string">&quot;cold err %d&quot;</span>, err);</span><br><span class="line">            <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (channel == LED_WARM_CHANNEL) &#123;</span><br><span class="line">        err = hal_pwm_para_chg(&amp;light_led_w, pwm_cfg);</span><br><span class="line">        <span class="keyword">if</span> (err) &#123;</span><br><span class="line">            LIGHT_DBG(<span class="string">&quot;warm err %d&quot;</span>, err);</span><br><span class="line">            <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">static</span> <span class="type">void</span> _led_set(<span class="type">uint8_t</span> onoff, <span class="type">uint16_t</span> actual, <span class="type">uint16_t</span> temperature)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">if</span>(onoff)</span><br><span class="line">    &#123;</span><br><span class="line">        hal_gpio_output_high(&amp;led_dev[<span class="number">0</span>]);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">else</span></span><br><span class="line">    &#123;</span><br><span class="line">        hal_gpio_output_low(&amp;led_dev[<span class="number">0</span>]);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h2 id="3-工程代码"><a href="#3-工程代码" class="headerlink" title="3. 工程代码"></a><font size=3>3. 工程代码</font></h2><p>详细的修改可以看这里：<a target="_blank" rel="noopener" href="https://github.com/sumumm/my-genie-bt-mesh-stack/commit/95e872ec87824cc2d2182c02f9df12c80eb6acc8">feat:支持天猫精灵和APP控制LED灯 · sumumm&#x2F;my-genie-bt-mesh-stack@95e872e (github.com)</a></p>
<h2 id="4-编译工程"><a href="#4-编译工程" class="headerlink" title="4. 编译工程"></a><font size=3>4. 编译工程</font></h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">aos make bluetooth.light_ctl_demo@tc825x</span><br></pre></td></tr></table></figure>

<h2 id="5-烧录程序"><a href="#5-烧录程序" class="headerlink" title="5. 烧录程序"></a><font size=3>5. 烧录程序</font></h2><img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV21-%E5%A4%A9%E7%8C%AB%E7%B2%BE%E7%81%B5IOT/LV03-04-%E5%A4%A9%E7%8C%AB%E8%93%9D%E7%89%99Mesh%E5%BC%80%E5%8F%91-05-%E5%A4%A9%E7%8C%AB%E7%B2%BE%E7%81%B5%E6%8E%A7%E5%88%B6%E6%99%BA%E8%83%BD%E7%81%AF/img/image-20231226074138417.png" alt="image-20231226074138417" />

<h2 id="6-实验现象"><a href="#6-实验现象" class="headerlink" title="6. 实验现象"></a><font size=3>6. 实验现象</font></h2><p>我们烧录完成后，需要重新用天猫精灵连接开发板，并且会有这些打印信息：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV21-%E5%A4%A9%E7%8C%AB%E7%B2%BE%E7%81%B5IOT/LV03-04-%E5%A4%A9%E7%8C%AB%E8%93%9D%E7%89%99Mesh%E5%BC%80%E5%8F%91-05-%E5%A4%A9%E7%8C%AB%E7%B2%BE%E7%81%B5%E6%8E%A7%E5%88%B6%E6%99%BA%E8%83%BD%E7%81%AF/img/image-20231226074305237.png" alt="image-20231226074305237" />

<p>这就说明我们的LED初始化过了。</p>
<h1 id="二、呼吸灯实现"><a href="#二、呼吸灯实现" class="headerlink" title="二、呼吸灯实现"></a><font size=3>二、呼吸灯实现</font></h1><h2 id="1-亮度模型？"><a href="#1-亮度模型？" class="headerlink" title="1. 亮度模型？"></a><font size=3>1. 亮度模型？</font></h2><p>我们可以到genie_app&#x2F;bluetooth&#x2F;mesh&#x2F;mesh_model&#x2F;inc目录找一些已经定义好的模型，像亮度模型是直接有的。</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV21-%E5%A4%A9%E7%8C%AB%E7%B2%BE%E7%81%B5IOT/LV03-04-%E5%A4%A9%E7%8C%AB%E8%93%9D%E7%89%99Mesh%E5%BC%80%E5%8F%91-05-%E5%A4%A9%E7%8C%AB%E7%B2%BE%E7%81%B5%E6%8E%A7%E5%88%B6%E6%99%BA%E8%83%BD%E7%81%AF/img/image-20231226074807652.png" alt="image-20231226074807652" />

<h2 id="2-代码修改"><a href="#2-代码修改" class="headerlink" title="2. 代码修改"></a><font size=3>2. 代码修改</font></h2><p>这里不写了，就是加上pwm就可以了。直接看github仓库本次提交的修改吧：<a target="_blank" rel="noopener" href="https://github.com/sumumm/my-genie-bt-mesh-stack/commit/b4d59785e40daf20bb916f7a28f5a9d560da21bb">feat:天猫精灵调节灯亮度实验 · sumumm&#x2F;my-genie-bt-mesh-stack@b4d5978 (github.com)</a></p>
<h2 id="3-云端属性添加"><a href="#3-云端属性添加" class="headerlink" title="3. 云端属性添加"></a><font size=3>3. 云端属性添加</font></h2><p>我们需要在功能定义中添加一个亮度属性：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV21-%E5%A4%A9%E7%8C%AB%E7%B2%BE%E7%81%B5IOT/LV03-04-%E5%A4%A9%E7%8C%AB%E8%93%9D%E7%89%99Mesh%E5%BC%80%E5%8F%91-05-%E5%A4%A9%E7%8C%AB%E7%B2%BE%E7%81%B5%E6%8E%A7%E5%88%B6%E6%99%BA%E8%83%BD%E7%81%AF/img/image-20231226075918166.png" alt="image-20231226075918166" />

<p>需要注意的是，这里实际传到我们使用的地方的时候，值范围是0~65536，而非0~100，另外这里是可以配置的：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV21-%E5%A4%A9%E7%8C%AB%E7%B2%BE%E7%81%B5IOT/LV03-04-%E5%A4%A9%E7%8C%AB%E8%93%9D%E7%89%99Mesh%E5%BC%80%E5%8F%91-05-%E5%A4%A9%E7%8C%AB%E7%B2%BE%E7%81%B5%E6%8E%A7%E5%88%B6%E6%99%BA%E8%83%BD%E7%81%AF/img/image-20231226080106730.png" alt="image-20231226080106730" style="zoom: 50%;" />

<h2 id="4-编译工程-1"><a href="#4-编译工程-1" class="headerlink" title="4. 编译工程"></a><font size=3>4. 编译工程</font></h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">aos make bluetooth.light_ctl_demo@tc825x</span><br></pre></td></tr></table></figure>

<h2 id="5-烧录程序-1"><a href="#5-烧录程序-1" class="headerlink" title="5. 烧录程序"></a><font size=3>5. 烧录程序</font></h2><p>跟前面配置一样。</p>
<h2 id="6-实验现象-1"><a href="#6-实验现象-1" class="headerlink" title="6. 实验现象"></a><font size=3>6. 实验现象</font></h2><p>首先初始化会有如下打印信息：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV21-%E5%A4%A9%E7%8C%AB%E7%B2%BE%E7%81%B5IOT/LV03-04-%E5%A4%A9%E7%8C%AB%E8%93%9D%E7%89%99Mesh%E5%BC%80%E5%8F%91-05-%E5%A4%A9%E7%8C%AB%E7%B2%BE%E7%81%B5%E6%8E%A7%E5%88%B6%E6%99%BA%E8%83%BD%E7%81%AF/img/image-20231226120612484.png" alt="image-20231226120612484" />

<p>我们的led和pwm都已初始化。然后使用天猫精灵连接设备，然后用语音“灯的亮度调到100”来调整灯的亮度。</p>

    </div>

    
    
    

    <footer class="post-footer">




    <div>
        
            <div style="text-align:center;color: #ccc;font-size:14px;">
            ----------本文结束
            <i class="fas fa-fan fa-spin" style="color: #FF1493; font-size: 1rem"></i>
            感谢您的阅读----------
            </div>
        
    </div>





  
  <div class="my_post_copyright"> 
    <p><span>文章标题:</span><a href="/post/29f1a077.html">LV03-04-天猫蓝牙Mesh开发-05-天猫精灵控制智能灯</a></p>
    <p><span>文章作者:</span><a href="/" title="欢迎访问 《苏木》 的学习笔记">苏木</a></p>
    <p><span>发布时间:</span>2024年01月07日 - 11:45</p>
    <p><span>最后更新:</span>2025年06月14日 - 00:25</p>
    <p><span>原始链接:</span><a href="/post/29f1a077.html" title="LV03-04-天猫蓝牙Mesh开发-05-天猫精灵控制智能灯">https://sumumm.github.io/post/29f1a077.html</a></p>
    <p><span>许可协议:</span><i class="fab fa-creative-commons"></i> <a rel="license" href= "https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">署名-非商业性使用-禁止演绎 4.0 国际</a> 转载请保留原文链接及作者。</p>  
  </div>
  


          <div class="post-tags">
              <a href="/tags/LV21-%E5%A4%A9%E7%8C%AB%E7%B2%BE%E7%81%B5IOT/" rel="tag"><i class="fa fa-tag"></i> LV21-天猫精灵IOT</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/post/54e18920.html" rel="prev" title="LV03-04-天猫蓝牙Mesh开发-06-厂商模型的使用">
                  <i class="fa fa-angle-left"></i> LV03-04-天猫蓝牙Mesh开发-06-厂商模型的使用
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/post/2c9a6a36.html" rel="next" title="LV03-04-天猫蓝牙Mesh开发-04-开发板对接天猫精灵">
                  LV03-04-天猫蓝牙Mesh开发-04-开发板对接天猫精灵 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 2017 – 
    <span itemprop="copyrightYear">2025</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">苏木</span>
  </div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
      <span>站点总字数：</span>
    <span title="站点总字数">3.7m</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span>站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">225:26</span>
  </span>
</div>




    <span id="sitetime"></span>
    <script defer language=javascript>
        function siteTime()
        {
            window.setTimeout("siteTime()", 1000);
            var seconds = 1000;
            var minutes = seconds * 60;
            var hours = minutes * 60;
            var days = hours * 24;
            var years = days * 365;
            var today = new Date();
            var todayYear = today.getFullYear();
            var todayMonth = today.getMonth()+1;
            var todayDate = today.getDate();
            var todayHour = today.getHours();
            var todayMinute = today.getMinutes();
            var todaySecond = today.getSeconds();
            /*==================================================
            Date.UTC() -- 返回date对象距世界标准时间(UTC)1970年1月1日午夜之间的毫秒数(时间戳)
            year        - 作为date对象的年份，为4位年份值
            month       - 0-11之间的整数，做为date对象的月份
            day         - 1-31之间的整数，做为date对象的天数
            hours       - 0(午夜24点)-23之间的整数，做为date对象的小时数
            minutes     - 0-59之间的整数，做为date对象的分钟数
            seconds     - 0-59之间的整数，做为date对象的秒数
            microseconds - 0-999之间的整数，做为date对象的毫秒数
            ==================================================*/
            var t1 = Date.UTC(2017, 
                              5, 
                              19, 
                              0, 
                              0, 
                              0); //北京时间
            var t2 = Date.UTC(todayYear,todayMonth,todayDate,todayHour,todayMinute,todaySecond);
            var diff = t2-t1;
            var diffYears = Math.floor(diff/years);
            var diffDays = Math.floor((diff/days)-diffYears*365);
            var diffHours = Math.floor((diff-(diffYears*365+diffDays)*days)/hours);
            var diffMinutes = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours)/minutes);
            var diffSeconds = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours-diffMinutes*minutes)/seconds);
            document.getElementById("sitetime").innerHTML="已在这里 "+diffYears+" 年 "+diffDays+" 天 "+diffHours+" 小时 "+diffMinutes+" 分钟 "+diffSeconds+" 秒";
        }
        siteTime();
    </script>



    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.umd.js" integrity="sha256-ytMJGN3toR+a84u7g7NuHm91VIR06Q41kMWDr2pq7Zo=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/lozad.js/1.16.0/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>




  <script src="/js/third-party/fancybox.js"></script>

  <script src="/js/third-party/pace.js"></script>


  




  

  <script class="next-config" data-name="enableMath" type="application/json">true</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>


 
        <div id="click-show-text"
            data-mobile = false
            data-text = 富强,民主,文明,和谐,自由,平等,公正,法制,爱国,敬业,诚信,友善
            data-fontsize = 15px
            data-random= false>
        </div>
       

      
        <script async src=https://cdn.jsdelivr.net/npm/hexo-next-mouse-effect@latest/click/showText.js></script>
      

      
    




    <script async src="/js/fancybox_param.js"></script>





<!-- APlayer本体 -->



</body>
</html>
